import React, { Component } from 'react';
import SplashScreen from 'react-native-splash-screen'
import { connect } from "react-redux";
import * as loginAction from '../user/login/LoginAction';
import NavigationUtil from "../../global/navigator/NavigationUtil";



class WelcomePage extends Component {
    componentDidMount() {
        //点击back返回的时候,props数据还在内存中,所以checkHasLogin需要有一个初始loading状态,只有在loading成功以后,才可以根据hasLogin进行跳转
        console.log("WelcomePage:componentDidMount", this.props);
        this.props.checkHasLogin();
    }
    componentWillUnmount() {
        console.log("WelcomePage:componentWillUnmount");
        this.timer && clearTimeout(this.timer);
    }
    shouldComponentUpdate(nextProps, nextState) {
        console.log("shouldComponentUpdate", nextProps);
        this.timer = setTimeout(() => {
            if (!nextProps.loading) {
                if (nextProps.hasLogin) {
                    console.log("WelcomePage:shouldComponentUpdate hasLogin = true");
                    NavigationUtil.resetTo(this.props, "Main");
                } else {
                    console.log("WelcomePage:shouldComponentUpdate hasLogin = false");
                    NavigationUtil.resetTo(this.props, "Login");
                }
                SplashScreen.hide();
            } else {
                console.log("still loading");
            }
        }, 200);
        return false;
    }
    render() {
        return null;
    }
}

export default connect(
    (state) => ({
        hasLogin: state.loginIn.hasLogin,
        loading: state.loginIn.loading
    }),
    (dispatch) => ({
        checkHasLogin: () => dispatch(loginAction.checkHasLogin())
    })
)(WelcomePage)